package org.example;
import java.sql.*;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;


public class RoomManager {
        public List<Room> getAllRooms() {
            List<Room> rooms = new ArrayList<>();
            String query = "SELECT * FROM rooms";

            try (Connection conn = DatabaseConnection.getConnection();
                 Statement stmt = conn.createStatement();
                 ResultSet rs = stmt.executeQuery(query)) {

                while (rs.next()) {
                    Room room = new Room();
                    room.setRoomId(rs.getInt("room_id"));
                    room.setRoomNumber(rs.getString("room_number"));
                    room.setRoomType(rs.getString("room_type"));
                    room.setPricePerNight(rs.getBigDecimal("price_per_night"));
                    room.setIsAvailable(rs.getBoolean("is_available"));
                    rooms.add(room);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

            return rooms;
        }
    public boolean bookRoom(int roomId, String guestName, Date checkInDate, Date checkOutDate) {
        String updateRoomAvailabilityQuery = "UPDATE rooms SET is_available = FALSE WHERE room_id = ?";
        String insertBookingQuery = "INSERT INTO bookings (guest_name, check_in_date, check_out_date, room_id) VALUES (?, ?, ?, ?)";

        boolean success = false;
        try (Connection conn = DatabaseConnection.getConnection();
             PreparedStatement updateStmt = conn.prepareStatement(updateRoomAvailabilityQuery);
             PreparedStatement insertStmt = conn.prepareStatement(insertBookingQuery)) {

            conn.setAutoCommit(false);

            updateStmt.setInt(1, roomId);
            updateStmt.executeUpdate();


            java.sql.Date sqlCheckInDate = new java.sql.Date(checkInDate.getTime());
            java.sql.Date sqlCheckOutDate = new java.sql.Date(checkOutDate.getTime());

            insertStmt.setString(1, guestName);
            insertStmt.setDate(2, sqlCheckInDate);
            insertStmt.setDate(3, sqlCheckOutDate);
            insertStmt.setInt(4, roomId);
            insertStmt.executeUpdate();

            conn.commit();
            success = true;

        } catch (SQLException e) {

            e.printStackTrace();
        }

        return success;
    }
    public boolean checkOutRoom(int roomId) {
        String updateRoomAvailabilityQuery = "UPDATE rooms SET is_available = TRUE WHERE room_id = ?";
        String deleteBookingQuery = "DELETE FROM bookings WHERE room_id = ?";

        boolean success = false;
        try (Connection conn = DatabaseConnection.getConnection();
             PreparedStatement updateStmt = conn.prepareStatement(updateRoomAvailabilityQuery);
             PreparedStatement deleteStmt = conn.prepareStatement(deleteBookingQuery)) {

            conn.setAutoCommit(false);

            updateStmt.setInt(1, roomId);
            updateStmt.executeUpdate();

            deleteStmt.setInt(1, roomId);
            deleteStmt.executeUpdate();

            conn.commit();
            success = true;

        } catch (SQLException e) {

            e.printStackTrace();
        }

        return success;
    }
}